package com.lemon.boot.service.impl;

import com.lemon.boot.common.base.BaseServiceImpl;
import com.lemon.boot.common.consts.ApiConst;
import com.lemon.boot.common.entity.LoginUserDetail;
import com.lemon.boot.common.enums.StatusEnum;
import com.lemon.boot.common.utils.JwtUtil;
import com.lemon.boot.dao.MenuDAO;
import com.lemon.boot.dto.MenuDTO;
import com.lemon.boot.po.MenuPO;
import com.lemon.boot.service.IMenuService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 菜单表 业务实现类
 *
 * @author 李猛
 * @since 2024-02-11
 */
@Log4j2
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuDAO, MenuPO, MenuDTO> implements IMenuService {
    @Override
    @Transactional
    public String add(MenuPO menuPO) {
        return this.addOrEdit(menuPO);
    }

    @Override
    @Transactional
    public String edit(MenuPO menuPO) {
        return this.addOrEdit(menuPO);
    }

    @Override
    @Transactional
    public String addOrEdit(MenuPO menuPO) {
        //1.判断是否是新增
        boolean isAdd = menuPO.getId() == null;

        //2.判断数据是否重复
        boolean exists = this.query().select("1")
                .and(item -> item.eq("name", menuPO.getName())
                        .or().eq("path", menuPO.getPath()))
                .and(!isAdd, item -> item.ne("id", menuPO.getId()))
                .last("limit 1").exists();
        if (exists) {
            return "数据重复，请重新输入";
        }

        return super.addOrEdit(menuPO);
    }

    @Override
    public List<String> getWebPathByCurrent() {
        //1.当前用户信息
        LoginUserDetail currentUser = JwtUtil.getCurrentUser();

        //2.查询web_file_path
        Map<String, Object> param = ApiConst.enableStatusAndUnableDelete();
        param.put("userId", currentUser.getUserId());
        param.put("tenantId", currentUser.getTenantId());
        return this.baseMapper.getWebFilePathByUserId(param);
    }
}